package com.game.animation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import com.game.basic.BasicRegion;
import com.game.config.Config;
import com.game.utils.Tools;

/**
 * 可以执行animation的对象都继承于此类
* @author oven:
* @version 创建时间：2012-11-6 上午9:03:21
* 
*/
public abstract class AnimationObject{
	//创建画笔
	protected Paint paint = Tools.createNewPaint();
	//引用对象不可改变
	protected final BasicRegion basicRegion=new BasicRegion();
	//调整的是此对象的region
	protected AnimationFix animationFix=new AnimationFix(basicRegion);
	protected Animation animation=null;
	protected boolean drawAnimation = true;
	protected boolean drawRegion = true;
	protected boolean stepAnimation=false;
	protected AnimationGradualchange gradualchange=null;
	protected boolean gradualchangIng=false;
	
	
	/**
	 * 添加渐变动画
	 * @param change
	 */
	public void addGradualChange(AnimationGradualchange change){
		if(gradualchange==null){
			gradualchange=change;
		}else{
			gradualchange.addGradualchange(change);
		}
	}
	
	/**
	 * 添加动画
	 * @param animation
	 */
	public void AddAnimation(Animation animation){
		if(this.animation==null){
			this.animation=animation;
		}else{
			this.animation.addAnimation(animation);
		}
	}
	
	/**
	 * 迭代动画
	 */
	public void startAnimation(){
		stepAnimation=true;
	}
	
	/**
	 * 暂停动画
	 */
	public void stopAnimation(){
		stepAnimation=false;
	}
	
	
	/**
	 * 绘制方法
	 * @param canvas
	 */
	public void draw(Canvas canvas){
		//绘制动画
		if(drawAnimation){
			canvas.save();
			if(animation!=null){
				//迭代动画
				if(stepAnimation){
					animation=animation.stepAnimation();
					//动画执行完毕
					if(animation==null){
						resetAnimationFix();
						drawAnimation=false;
						stepAnimation=false;
					}
				}
			}
			if(animation!=null){
				canvas.translate(animationFix.getTransx(),animationFix.getTransy());
				canvas.scale(1+animationFix.getScalex(),
							 1+animationFix.getScaley(),
							 getCenterX(),
							 getCenterY());
			}
			//调用子类的paint方法
			paint(canvas);
			canvas.restore();
		}else{
			//调用子类的paint方法
			paint(canvas);
		}
		if(Config.DRAW_REGION&&drawRegion){
			//绘制区域
			drawRegion(canvas);
		}
		if(gradualchange!=null){
			gradualchange.stepGradulchange(canvas);
		}
	}
	
	/**
	 * 重置
	 */
	public void resetAnimationFix(){
		animationFix.reset();
	}
	
	/**
	 * 绘制运动区域
	 * @param canvas
	 */
	protected void drawRegion(Canvas canvas){
		int color = paint.getColor();
		int alpha = paint.getAlpha();
		paint.setColor(Color.RED);
		paint.setAlpha(80);
		
		drawRegion(canvas,paint,getRegion());
		
		paint.setColor(color);
		paint.setAlpha(alpha);
	}
	
	/**
	 * 绘制区域
	 * @param canvas
	 * @param paint
	 * @param region
	 */
	protected void drawRegion(Canvas canvas,Paint paint,float[] region){
		canvas.drawLine(region[0], region[1], region[2], region[1],paint);
		canvas.drawLine(region[0], region[1], region[0], region[3],paint);
		canvas.drawLine(region[2], region[3], region[2], region[1],paint);
		canvas.drawLine(region[2], region[3], region[0], region[3],paint);
		canvas.drawRect(region[0],region[1],region[2], region[3], paint);
	}
	
	/**
	 * 获区域
	 * @return
	 */
	public  float[] getRegion(){
		if(drawAnimation){
			return getRegionWithAnimation();
		}else{
			return basicRegion.getRegionXY();
		}
	}
	
	/**
	 * 获取区域
	 * @return
	 */
	public  RectF getRegionRectF(){
		float[] region = getRegion();
		RectF rectf = new RectF(region[0],region[1],region[2],region[3]);
		return rectf;
	}
	
	/**
	 * 获取区域  受animation影响
	 * @return
	 */
	public float[] getRegionWithAnimation(){
		float[] result=null;
		if(animation!=null){
			result=new float[4];
			result[0]=getCenterX()+animationFix.getTransx()-getWidth()*(1+animationFix.getScalex())/2;
			result[1]=getCenterY()+animationFix.getTransy()-getHeight()*(1+animationFix.getScaley())/2;
			result[2]=getCenterX()+animationFix.getTransx()+getWidth()*(1+animationFix.getScalex())/2;
			result[3]=getCenterY()+animationFix.getTransy()+getHeight()*(1+animationFix.getScaley())/2;
		}else{
			result = basicRegion.getRegionXY();
			
		}
		return result;
	}
	
	
	/**
	 * 获取精灵的中心点
	 * @return
	 */
	public float[] getCenter(){
		float[] center = basicRegion.getCenterPoint();
		return center;
	}
	
	/**
	 * 如果是用于绘制,不用此方法
	 * @return
	 */
	public float[] getCenterWithAnimation(){
		float[] center = basicRegion.getCenterPoint();
		if(animation!=null){
			center[0]=center[0]+animationFix.getTransx();
			center[1]=center[1]+animationFix.getTransy();
		}
		return center;
	}
	
	public float getCenterX(){
		return basicRegion.getCenterPointX();
	}
	
	public float getCenterY(){
		return basicRegion.getCenterPointY();
	}
	
	public float getButtom(){
		return basicRegion.getPosty()+basicRegion.getHeight();
	}
	
	
	public float getPostX() {
		return basicRegion.getPostx();
	}
	
	/**
	 * 如果是用于绘制,不用此方法
	 * @return
	 */
	public float getPostXwithAnimation(){
		if(animation!=null){
			return basicRegion.getPostx()+animationFix.getTransx();
		}
		return basicRegion.getPostx();
	}

	public void setPostX(float postX) {
		basicRegion.setPostx(postX);
	}

	public float getPostY() {
		return basicRegion.getPosty();
	}
	
	/**
	 * 如果是用于绘制,不用此方法
	 * @return
	 */
	public float getPostYwithAnimation() {
		if(animation!=null){
			return basicRegion.getPosty()+animationFix.getTransy();
		}
		return basicRegion.getPosty();
	}

	public void setPostY(float postY) {
		basicRegion.setPosty(postY);
	}
	
	/**
	 * 设置中心点
	 * @param x
	 * @param y
	 */
	public void setCenterPoint(float x,float y){
		basicRegion.setPostWithCenterPoint(x, y);
	}

	public void setCenterPointX(float x){
		basicRegion.setPostxWithCenterPoint(x);
		
	}
	
	public void setCenterPointY(float y){
		basicRegion.setPostyWithCenterPoint(y);
	}
	
	public void setBottom(float y){
		basicRegion.setPostyWithBottom(y);
	}
	
	public float getWidth() {
		return basicRegion.getWidth();
	}

	public void setWidth(float width) {
		basicRegion.setWidth(width);
	}
	
	public void setWidth(Bitmap bmp) {
		basicRegion.setWidth(bmp.getWidth());
	}

	public float getHeight() {
		return basicRegion.getHeight();
	}

	public void setHeight(float height) {
		basicRegion.setHeight(height);
	}
	
	public void setHeight(Bitmap bmp) {
		basicRegion.setHeight(bmp.getHeight());
	}
	
	public void addPostx(float x){
		basicRegion.addPostx(x);
	}
	
	public void addPosty(float y){
		basicRegion.addPosty(y);
	}
	
	public void addPosx(){
		addPostx(1);
	}
	public void addPosty(){
		addPosty(1);
	}
	
	public void minPosx(){
		addPostx(-1);
	}
	public void minPosty(){
		addPosty(-1);
	}
	
	/**
	 * 抽象的paint方法,由子类继承实现
	 * @param canvas
	 */
	protected abstract void paint(Canvas canvas);


	public AnimationFix getAnimationFix() {
		return animationFix;
	}

	public void setAnimationFix(AnimationFix animationFix) {
		this.animationFix = animationFix;
	}

	public Animation getAnimation() {
		return animation;
	}

	public void setAnimation(Animation animation) {
		this.animation = animation;
	}

	public BasicRegion getBasicRegion() {
		return basicRegion;
	}

	public boolean isDrawAnimation() {
		return drawAnimation;
	}

	public boolean isStepAnimation() {
		return stepAnimation;
	}

	public void setDrawAnimation(boolean drawAnimation) {
		this.drawAnimation = drawAnimation;
	}

	public void setStepAnimation(boolean stepAnimation) {
		this.stepAnimation = stepAnimation;
	}

	public boolean isGradualchangIng() {
		return gradualchangIng;
	}

	public void setGradualchangIng(boolean gradualchangIng) {
		this.gradualchangIng = gradualchangIng;
	}
}

